home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / PRTPerVertex / SimpleLighting.fx < prev    next >
Encoding:
Text File  |  2004-09-27  |  4.3 KB  |  132 lines

  1. //-----------------------------------------------------------------------------
  2. // File: SimpleLighting.fx
  3. //
  4. // Desc: The technique SimpleLighting renders the
  5. //         scene with standard N.L lighting.
  6. // 
  7. // Copyright (c) Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9.  
  10.  
  11. //-----------------------------------------------------------------------------
  12. // Global variables
  13. //-----------------------------------------------------------------------------
  14. float4x4 g_mWorldViewProjection;
  15. float4x4 g_mWorldInv;
  16. texture  AlbedoTexture;
  17.  
  18. float4 MaterialDiffuseColor = { 1.0f, 1.0f, 1.0f, 1.0f };    
  19. float4 LightsDir[10]        : register(c10);
  20. float4 Light1Dir            : register(c30);
  21. float4 Light2Dir            : register(c31);
  22. float4 LightsDiffuse[10]    : register(c20);
  23.  
  24.  
  25. //-----------------------------------------------------------------------------
  26. // Texture samplers
  27. //-----------------------------------------------------------------------------
  28. sampler AlbedoSampler = 
  29. sampler_state
  30. {
  31.     Texture = <AlbedoTexture>;
  32.     MipFilter = LINEAR;
  33.     MinFilter = LINEAR;
  34.     MagFilter = LINEAR;
  35. };
  36.  
  37.  
  38. //-----------------------------------------------------------------------------
  39. // Vertex shader output structure
  40. //-----------------------------------------------------------------------------
  41. struct VS_OUTPUT
  42. {
  43.     float4 Position  : POSITION;    // position of the vertex
  44.     float4 Diffuse   : COLOR0;      // diffuse color of the vertex
  45.     float2 TextureUV : TEXCOORD0;   // typical texture coords stored here
  46. };
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Renders with standard N.L lighting
  51. //-----------------------------------------------------------------------------
  52. VS_OUTPUT SimpleLightingVS( float4 vPos : POSITION, 
  53.                             float4 vNormal : NORMAL,
  54.                             float2 vTexCoord0 : TEXCOORD0,
  55.                             uniform bool bTexture )
  56. {
  57.     VS_OUTPUT Output;
  58.     
  59.     // Output the vetrex position in projection space
  60.     Output.Position = mul(vPos, g_mWorldViewProjection);    
  61.   
  62.     Output.Diffuse = 0;
  63.     for( int i=0; i<10; i++ )    
  64.     {
  65.         float3 vLightObjSpace = mul(LightsDir[i], g_mWorldInv);
  66.         Output.Diffuse += LightsDiffuse[i] * max(0, dot(vNormal, vLightObjSpace ));
  67.     }
  68.     
  69.     Output.Diffuse *= MaterialDiffuseColor;
  70.  
  71.     if( bTexture )
  72.         Output.TextureUV = vTexCoord0;
  73.     else
  74.         Output.TextureUV = 0;
  75.     
  76.     return Output;
  77. }
  78.  
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Pixel shader output structure
  82. //-----------------------------------------------------------------------------
  83. struct PS_OUTPUT
  84. {   
  85.     float4 RGBColor : COLOR0;  // Pixel color    
  86. };
  87.  
  88.  
  89. //-----------------------------------------------------------------------------
  90. // Trival pixel shader
  91. //-----------------------------------------------------------------------------
  92. PS_OUTPUT StandardPS( VS_OUTPUT In,
  93.                       uniform bool bTexture ) 
  94.     PS_OUTPUT Output;
  95.  
  96.     if( bTexture )
  97.         Output.RGBColor = tex2D(AlbedoSampler, In.TextureUV) * In.Diffuse;
  98.     else
  99.         Output.RGBColor = In.Diffuse;
  100.  
  101.     return Output;
  102. }
  103.  
  104.  
  105. //-----------------------------------------------------------------------------
  106. // Renders with standard N.L lighting with an albedo texture
  107. //-----------------------------------------------------------------------------
  108. technique RenderWithNdotL
  109. {
  110.     pass P0
  111.     {          
  112.         VertexShader = compile vs_1_1 SimpleLightingVS( true ); // trival vertex shader (could use FF if desired)
  113.         PixelShader  = compile ps_1_1 StandardPS( true );       // trival pixel shader (could use FF if desired)
  114.     }
  115. }
  116.  
  117.  
  118. //-----------------------------------------------------------------------------
  119. // Renders with standard N.L lighting w/o an albedo texture
  120. //-----------------------------------------------------------------------------
  121. technique RenderWithNdotLNoAlbedo
  122. {
  123.     pass P0
  124.     {          
  125.         VertexShader = compile vs_1_1 SimpleLightingVS( false ); // trival vertex shader (could use FF if desired)
  126.         PixelShader  = compile ps_1_1 StandardPS( false );       // trival pixel shader (could use FF if desired)
  127.     }
  128. }
  129.  
  130.  
  131.